package ext.acae2.dms.web.output;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

import ext.acae2.util.FileUtil;

import wt.log4j.LogR;

public class TozipProcessor {

	private static final Logger LOGGER = LogR.getLogger(TozipProcessor.class
			.getName());

	public File tozip(String downloadPath, String fileName) {
		LOGGER.debug("> Start tozip");

		Project pj = new Project();
		Zip zip = new Zip();
		zip.setProject(pj);

		File zipFile = new File(downloadPath + File.separator + fileName
				+ ".zip");
		zip.setDestFile(zipFile);

		FileSet fileSet = new FileSet();
		fileSet.setProject(pj);
		fileSet.setDir(new File(downloadPath + File.separator + fileName));
		
		FileSet file = new FileSet();
		file.setProject(pj);
		file.setFile(new File(downloadPath + File.separator + fileName
				+ ".xml"));
		
		zip.addFileset(fileSet);
		zip.addFileset(file);
		zip.execute();

		LOGGER.debug("> End tozip");
		return zipFile;
	}
	
	public boolean delzip(String downloadPath, String fileName) {
		
		File zipFile = new File(downloadPath + File.separator + fileName
				+ ".zip");
		boolean delzip = zipFile.delete();
		if(delzip != true){
			return false;
		}
		
		File xmlFile = new File(downloadPath + File.separator + fileName
				+ ".xml");
		boolean delxml = xmlFile.delete();
		if(delxml != true){
			return false;
		}
		
		File file = new File(downloadPath + File.separator + fileName);
		boolean delfile = FileUtil.deleteDir(file);
		if(delfile != true){
			return false;
		}
		
		return true;
	}
	
}
